{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3468d37",
   "metadata": {},
   "outputs": [],
   "source": [
    "import akshare as ak\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sqlalchemy import create_engine"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f5312b50",
   "metadata": {},
   "outputs": [],
   "source": [
    "engine = create_engine(\"postgresql://postgres:123456@localhost:5432/FDP\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c99970df",
   "metadata": {},
   "outputs": [],
   "source": [
    "sh_df = ak.stock_info_sh_name_code()\n",
    "sz_df = ak.stock_info_sz_name_code()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "31cf0a08",
   "metadata": {},
   "outputs": [],
   "source": [
    "sh = sh_df['证券代码'].values\n",
    "sz = sz_df['A股代码'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b0ecc8ea",
   "metadata": {},
   "outputs": [],
   "source": [
    "codes = np.concatenate([sh, sz],axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e37b50e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_data(i: str):\n",
    "    hist = ak.stock_zh_a_hist(symbol=i,period='daily',adjust='hfq')\n",
    "    hist.to_sql(\"hist_hfq\", engine, if_exists='append', index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "313de4c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "for code in codes:\n",
    "    get_data(code)\n",
    "print('Done')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1f77609",
   "metadata": {},
   "outputs": [],
   "source": [
    "get_data('600519')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fdp",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
